import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const store = new Vuex.Store({
  state: {
    user: JSON.parse(localStorage.getItem('user')) || null,
    books: [
      { id: 1, title: "红楼梦", author: "曹雪芹", price: 45.8, stock: 100, cover: '/images/红.jpg', description: '中国古典文学名著' },
      { id: 2, title: "活着", author: "余华", price: 28.5, stock: 50, cover: '/images/活.jpg', description: '讲述了在大时代背景下小人物的命运' },
      { id: 3, title: "围城", author: "钱钟书", price: 36.0, stock: 80, cover: '/images/围.jpg', description: '一部关于婚姻和人生的讽刺小说' },
      { id: 4, title: "三体", author: "刘慈欣", price: 56.0, stock: 150, cover: '/images/三.jpg', description: '中国科幻文学的经典之作' },
      { id: 5, title: "神经漫游者", author: "威廉·吉布森", price: 42.9, stock: 30, cover: '/images/神经.jpg', description: '赛博朋克文学的开山之作' },
      { id: 6, title: "沙丘", author: "弗兰克·赫伯特", price: 65.0, stock: 20, cover: '/images/沙.jpg', description: '科幻史上的经典巨著' },
      { id: 7, title: "史记", author: "司马迁", price: 68.0, stock: 120, cover: '/images/史.jpg', description: '中国第一部纪传体通史' },
      { id: 8, title: "资治通鉴", author: "司马光", price: 75.5, stock: 90, cover: '/images/资.jpg', description: '编年体通史巨著' },
      { id: 9, title: "万历十五年", author: "黄仁宇", price: 32.8, stock: 180, cover: '/images/万.jpg', description: '以独特视角解读明朝历史' },
      { id: 10, title: "艺术的故事", author: "贡布里希", price: 228.0, stock: 50, cover: '/images/艺.jpg', description: '艺术史领域的经典著作' },
      { id: 11, title: "现代艺术150年", author: "威尔·贡培兹", price: 88.0, stock: 70, cover: '/images/现代.jpg', description: '了解现代艺术发展的佳作' },
      { id: 12, title: "写给大家的西方美术史", author: "蒋勋", price: 56.0, stock: 100, cover: '/images/写给.jpg', description: '通俗易懂的西方美术史读物' },
      { id: 13, title: "时间简史", author: "霍金", price: 45.0, stock: 200, cover: '/images/时间.jpg', description: '全球科学著作的里程碑' },
      { id: 14, title: "人类简史", author: "尤瓦尔·赫拉利", price: 68.0, stock: 150, cover: '/images/人类.jpg', description: '从全新视角讲述人类历史' },
      { id: 15, title: "如何阅读一本书", author: "莫提默·J. 艾德勒", price: 38.0, stock: 80, cover: '/images/如何.jpg', description: '阅读方法的经典指南' }
    ],
    cartItems: JSON.parse(localStorage.getItem('cartItems')) || [],
    orders: JSON.parse(localStorage.getItem('orders')) || []
  },
  mutations: {
    setUser(state, user) {
      state.user = user
      localStorage.setItem('user', JSON.stringify(user))
    },
    clearUser(state) {
      state.user = null
      localStorage.removeItem('user')
    },
    addToCart(state, book) {
      const existingItem = state.cartItems.find(item => item.id === book.id)
      if (existingItem) {
        if (existingItem.quantity < book.stock) {
          existingItem.quantity++
        } else {
          throw new Error('库存不足')
        }
      } else {
        state.cartItems.push({ ...book, quantity: 1 })
      }
      localStorage.setItem('cartItems', JSON.stringify(state.cartItems))
    },
    removeFromCart(state, bookId) {
      state.cartItems = state.cartItems.filter(item => item.id !== bookId)
      localStorage.setItem('cartItems', JSON.stringify(state.cartItems))
    },
    updateCartItemQuantity(state, { bookId, quantity }) {
      const item = state.cartItems.find(item => item.id === bookId)
      if (item) {
        const book = state.books.find(b => b.id === bookId)
        if (book && quantity <= book.stock) {
          item.quantity = quantity
          localStorage.setItem('cartItems', JSON.stringify(state.cartItems))
        }
      }
    },
    clearCart(state) {
      state.cartItems = []
      localStorage.removeItem('cartItems')
    },
    addOrder(state, order) {
      state.orders.push(order)
      localStorage.setItem('orders', JSON.stringify(state.orders))
    }
  },
  actions: {
    login({ commit }, user) {
      commit('setUser', user)
    },
    logout({ commit }) {
      commit('clearUser')
      commit('clearCart')
    },
    addToCart({ commit }, book) {
      return new Promise((resolve, reject) => {
        try {
          commit('addToCart', book)
          resolve()
        } catch (error) {
          reject(error.message)
        }
      })
    }
  },
  getters: {
    isLoggedIn: state => !!state.user,
    cartTotal: state => {
      return state.cartItems.reduce((total, item) => {
        const price = parseFloat(item.price)
        const quantity = parseInt(item.quantity)
        return total + (price * quantity)
      }, 0)
    },
    cartItemCount: state => {
      return state.cartItems.reduce((count, item) => count + item.quantity, 0)
    }
  }
})

export default store 